package com.arcgis.app.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcgis.app.R;
import com.arcgis.app.utils.NavigationUtil;
import com.google.android.material.button.MaterialButton;

public class CommonHeaderView extends LinearLayout {
    private Button mBackBtn;
    private TextView textView_middle;
    private TextView textView_right;
    private ImageView iv_right_icon;

    private OnClickListener rightClickListener;

    public void setLeftClickListener(OnClickListener leftClickListener) {
        this.leftClickListener = leftClickListener;
    }

    private OnClickListener leftClickListener;

    private OnClickListener middleClickListener;

    public CommonHeaderView(Context context) {
        super(context);
    }

    public CommonHeaderView(Context context, AttributeSet attrs) {

        super(context, attrs);

        View inflate = inflate(getContext(), R.layout.common_header, this);

        mBackBtn = inflate.findViewById(R.id.header_back_btn);

        mBackBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (leftClickListener == null) {
                    NavigationUtil.pop((Activity) getContext());
                } else {

                    leftClickListener.onClick(v);
                }

            }
        });

        textView_middle = findViewById(R.id.tv_middle_title);

        textView_right = findViewById(R.id.tv_right_title);

        iv_right_icon = findViewById(R.id.iv_right_icon);


        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonHeaderView);

        if (attributes != null) {
            String rightText = attributes.getString(R.styleable.CommonHeaderView_m_right_text);
            int rightIcon = attributes.getResourceId(R.styleable.CommonHeaderView_m_right_icon, -1);
            String title = attributes.getString(R.styleable.CommonHeaderView_m_title);

            int m_title_color = attributes.getColor(R.styleable.CommonHeaderView_m_title_color, getContext().getResources().getColor(R.color.leader_home_btn_unselected));
            Boolean show_back = attributes.getBoolean(R.styleable.CommonHeaderView_m_show_back, true);

            int m_title_size = attributes.getInt(R.styleable.CommonHeaderView_m_title_size, 16);
            if (rightText != null) {
                textView_right.setVisibility(View.VISIBLE);
                textView_right.setText(rightText);
                iv_right_icon.setVisibility(View.INVISIBLE);
            } else {
                iv_right_icon.setVisibility(View.INVISIBLE);

                if (rightIcon != -1) {
                    iv_right_icon.setVisibility(View.VISIBLE);

                    Drawable d = context.getDrawable(rightIcon);

                    iv_right_icon.setImageDrawable(d);
                }

                textView_right.setVisibility(View.INVISIBLE);
            }
            if (title != null) {
                textView_middle.setText(title);
            }

            if (m_title_color != 0) {

                textView_middle.setTextColor(m_title_color);
            }

            if (m_title_size != 0) {

                textView_middle.setTextSize(m_title_size);
            }


            if (show_back != null) {

                if (show_back) {
                    mBackBtn.setVisibility(View.VISIBLE);
                } else {
                    mBackBtn.setVisibility(View.INVISIBLE);
                }

            } else {
                mBackBtn.setVisibility(View.VISIBLE);
            }
        }

    }

    public CommonHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setRightClickListener(OnClickListener rightClickListener) {

        this.rightClickListener = rightClickListener;

        textView_right.setOnClickListener(this.rightClickListener);

        iv_right_icon.setOnClickListener(this.rightClickListener);

    }

    public void setMiddleClickListener(OnClickListener middleClickListener) {

        this.middleClickListener = middleClickListener;

        textView_middle.setOnClickListener(this.middleClickListener);
    }

    public void setMiddleTitle(String text) {
        textView_middle.setText(text);
    }

    public void setRightTitle(String text) {
        textView_right.setText(text);
    }

    public int getResource(String imageName) {
        Context ctx = getContext();
        int resId = getResources().getIdentifier(imageName, "drawable", ctx.getPackageName());
        //如果没有在"mipmap"下找到imageName,将会返回0
        return resId;
    }
}